<template>
  <div class="register">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" width="300" id="registerform">
    <el-form-item label="用户名" prop="user">
         <el-input type="text" v-model="ruleForm.user" autocomplete="off" name="userName"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off" name="passWord"></el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
    </el-form-item>
    <div class="slide">
        <slide-verify :l="42"
                    :r="10"
                    :w="310"
                    :h="155"
                    slider-text="向右滑动"
                    @success="onSuccess"
                    @fail="onFail"
                    @refresh="onRefresh"
                    ></slide-verify>
        <div>{{msg}}</div>
    </div>
    <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" >注册</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <a href="/login" class="already">已有帐号？登录</a>
    </el-form-item>
    </el-form>
  </div>
  
</template>

<script>

export default {
    data(){
        var validateuser = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return { 
           ruleForm: { //注册登录验证
            pass: '',
            checkPass: '',
            user: ''
            },
            rules: {
                pass: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                user: [
                    { validator: validateuser, trigger: 'blur' }
                ]
            },
            msg: '',
            isok:0
        }
    },
    methods: {
        onSuccess(){
            this.isok = 1,
            this.msg = '验证成功'
        },
        onFail(){
            this.isok = 0,
            this.msg = '验证失败'
        },
        onRefresh(){
            this.isok = 0
            this.msg = ''
        },
        submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              if(this.isok == 1){
                  //请求axios
                  this.$axios.post('/api/cmsusers/register',{
                      userName :this.ruleForm.user,
                      passWord: this.ruleForm.pass
                  }).then((res)=>{
                      // console.log(res);
                      if(res.data.code == 0){
                          this.$message({
                               message: '恭喜你，注册成功',
                                type: 'success'
                          });
                        this.$router.push('/login')
                      }else{
                        this.$message({
                            message: '注册失败，请重新注册',
                            type: 'error'
                        });
                      }
                  });
              }else{
                   console.log('error submit!!');
                  return false;
              }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }

}
</script>

<style>
html{
    padding: 0;
    margin: 0;
}
html{
    background: rgb(245,245,245);
}
.register{
    width: 400px;
    /* text-align: center; */
    margin: 100px auto;
    /* border: 1px solid pink; */
    padding: 50px;
}
.slide{
    text-align: center;
    margin: 20px 0;
    margin-left:90px;
}
.already{
    margin: 10px;
}


</style>